<?php

namespace App\Admin\Actions\Post;

use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Shipments extends RowAction
{
    public $name = '发货';

    public function handle(Model $model, Request $request)
    {

        if ($model->status != 2){
            return $this->response()->topRight()->error('该订单状态不属于待发货');
        }
        $courierSn = $request->get('courierSn');

        if (!empty($courierSn)) {
            $model->courier_sn = $courierSn;
        }

        $model->status = 3;

        if($model->save()){
            return $this->response()->topRight()->success('发货成功')->refresh();
        }
        return $this->response()->topRight()->error('发货失败');
    }

    public function form()
    {

        $this->text('courierSn', '快递单号')->attribute('autocomplete', 'off');
    }

}
